package Utils;

import java.awt.List;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.sun.accessibility.internal.resources.accessibility;
import com.sun.org.apache.bcel.internal.generic.DMUL;

import play.db.DB;
import play.mvc.Scope.Flash;

public class ClassUtilsDB {
	
	//Pegar o proximo valor do campo tipo long a ser inserido na tabela
	public static int next (String campo,String tabela)
	{

		String sql = "select (max(" + campo + ") + 1) as max from " + tabela;
		int result = 0;
		
		try
		{
			ResultSet rs = DB.executeQuery(sql);
		
			while ( rs.next() )
			{
				result = rs.getInt("max");
			}
		}
		catch(Exception e)
		{
			
		}
		
		if (result == 0)
			return 1;
		else
			return result;
	}
	
	public static String nextConta()
	{

		String sql = "select (max(CodConta) + 1) as max from Conta";
		String result = "";
		
		try
		{
			ResultSet rs = DB.executeQuery(sql);
		
			while ( rs.next() )
			{
				result = rs.getString("max");
			}
		}
		catch(Exception e)
		{
			
		}
		
		return result;
	}
	
	
	public static long next(String campoInc,String tabela,String query, Object... valores) throws Exception
	{
		long saida = 0;
		String sql = "SELECT (MAX(" + campoInc + ") + 1) as max FROM " + tabela;
		
		if(query != "" && valores != null)
		{
			sql += " WHERE " + String.format(query, valores);				
		}
		else
			if(query != "" && valores != null)
			{
				throw new Exception("Parametros invalidos!");
			}
		
		try
		{
			ResultSet rs = DB.executeQuery(sql);
			
			while( rs.next() )
			{
				saida = rs.getLong("max");
			}
			
		}
		catch(Exception e)
		{
			
		}
	
		if(saida == 0)
			saida = 1;
		
		return saida;	
	}


}
